package jm.mq.boot_mq.conf;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description ttl 队列配置文件
 * @date 2022/3/10 9:53
 */
//@Configuration
public class TtlQueueConfig {

    // 普通交换机
    private static final String X_EXCHANGE = "X";

    // 死信交换机
    private static final String DEAD_LETTER_Y_EXCHANGE = "Y";

    // 普通队列
    private static final String QUEUE_A = "QA";
    private static final String QUEUE_B = "QB";

    // 死信队列
    private static final String DEAD_LETTER_QUEUE_D = "QD";

    // 设置普通交换机
    @Bean("xExchange")
    public DirectExchange xExchange(){
        return new DirectExchange(X_EXCHANGE);
    }

    // 设置死信交换机
    @Bean("yExchange")
    public DirectExchange yExchange(){
        return new DirectExchange(DEAD_LETTER_Y_EXCHANGE);
    }

    // 设置普通队列
    @Bean("queueA")
    public Queue aQueue(){
        Map<String ,Object> argument = new HashMap<>();
        // 设置死信交换机
        argument.put("x-dead-letter-exchange",DEAD_LETTER_Y_EXCHANGE);
        // 设置死信RoutingKey
        argument.put("x-dead-letter-routing-key","YD");
        // 设置过期时间
        argument.put("x-message-ttl",10000);
        return QueueBuilder.durable(QUEUE_A).withArguments(argument).build();
    }

    @Bean("queueB")
    public Queue bQueue(){
        Map<String ,Object> argument = new HashMap<>();
        // 设置死信交换机
        argument.put("x-dead-letter-exchange",DEAD_LETTER_Y_EXCHANGE);
        // 设置死信RoutingKey
        argument.put("x-dead-letter-routing-key","YD");
        // 设置过期时间
        argument.put("x-message-ttl",40000);
        return QueueBuilder.durable(QUEUE_B).withArguments(argument).build();
    }


    // 设置死信队列
    @Bean("queueD")
    public Queue dQueue(){
        return QueueBuilder.durable(DEAD_LETTER_QUEUE_D).build();
    }

    /**
     * 队列A绑定交换机X
     * @return
     */
    @Bean
    public Binding queueABindingX(@Qualifier("queueA") Queue queueA,
                                  @Qualifier("xExchange") DirectExchange xExchange) {
        return BindingBuilder.bind(queueA).to(xExchange).with("XA");
    }

    /**
     * 队列B绑定交换机X
     * @return
     */
    @Bean
    public Binding queueBBindingX(@Qualifier("queueB") Queue queueB,
                                  @Qualifier("xExchange") DirectExchange xExchange) {
        return BindingBuilder.bind(queueB).to(xExchange).with("XB");
    }

    /**
     * 队列D绑定交换机Y
     * @return
     */
    @Bean
    public Binding queueDBindingY(@Qualifier("queueD") Queue queueD,
                                  @Qualifier("yExchange") DirectExchange xExchange) {
        return BindingBuilder.bind(queueD).to(xExchange).with("YD");
    }

}
